home *** CD-ROM | disk | FTP | other *** search
- /**
- --
- -- App: Bitmap Shape with Clip
- --
- --
- -- File: Bitmap Shape with Clip.c
- --
- --
- -- Comments: This code the ability of QuickDraw GX to clip any shape with any geometric shape.
- -- In this case, we retrieve a bitmap shape from the resource fork of the application,
- -- and clip it with a text shape - "BEACH". We collect both shapes into a GX picture,
- -- thereby allowing us to make one call to draw both shapes.retrieves a bitmap shape
- -- from the resource fork of the app. It creates the text shape
- --
- --
- -- 4/96 bob Updated #includes to support changed GX Library names.
- -- Changed fixed to Fixed.
- -- Updated the note regarding the files needed to run, and copyright date.
- --
- -- Version: 1.0 4/94: added the capability to dynamically determine the amount of
- -- scaling required to make the clip shape clip the entire bitmap
- -- shape.
- --
- -- 3/93: created
- --
- --
- -- Components: Bitmap Shape with Clip.c
- -- graphics shell.c
- -- graphics shell.h
- --
- --
- -- QuickDraw GX
- -- Libraries
- -- Used: This application uses the following QuickDraw GX library code files:
- -- "ColorLibrary.c", "FontLibrary.c", "GraphicsDebugLibrary.c",
- -- "QDLibrary.c", and "TransformLibrary.c".
- --
- --
- -- Notes: 1) Print this file in landscape for the best results
- -- 2) If you are using THINK C v5.x, I have added THINK markers to navigate the code.
- -- 3) This code was adapted from the "One Rectangle" QuickDraw GX sample.
- --
- --
- -- Author: Pete "Luke" Alexander
- -- Developer Technical Support
- -- AppleLink: DEVSUPPORT
- --
- --
- -- ©1992 - 1996 Apple Computer, Inc.
- -- All rights reserved.
- --
- **/
-
- #include <events.h>
- #include <windows.h>
-
- #include "FontLibrary.h"
- #include <GXErrors.h>
- #include "GraphicsLibraries.h"
- #include <GXEnvironment.h>
- #include "QDLibrary.h"
- #include "graphics shell.h"
-
- #define kCheckBitmapShape
-
- //
- // Set up the title and size of the window
- //
- Str255 gWindowTitle = "\p Bitmap Shape with Clip ";
- Rect gWindowQDRect = {50, 20, 345, 455};
-
- //
- // gGraphicsHeapSize sets the size of the graphics gxHeap created by calling the GXNewGraphicsClient routine
- // in main () within graphics shell.c. You can determine the amount of graphics gxHeap required by using GraphicsBug.
- // With gGraphicsHeapSize set to 48k,I had 6 free blocks left in the graphics gxHeap and
- // I was not receiving any memory related warnings or notices from GX....
- //
- long gGraphicsHeapSize = 48;
-
- gxShape gthePicture;
-
-
-
- /*------ DoInitialization ---------------------------------------------------------------------------------*/
-
- void DoInitialization(gWindow)
- WindowPtr gWindow;
- {
- gxShape newClipShape, clipShapeOutline;
- gxShape tempBitmapShape;
- float xScaleFactor, yScaleFactor;
- Fixed clipShapeWidth,clipShapeHeight;
- Fixed bitmapWidth, bitmapHeight;
- gxRectangle bitmapBounds, clipShapeBounds;
-
-
- InitCommonColors ();
-
- gthePicture = GXNewShape(gxPictureType);
-
- //
- // Retrieve the bitmap shape form the resource fork of the application. With the "debugging" init installed, we
- // can check to see if the shape was retrieved correctly by calling the shape validation routine. If the shape is
- // not valid, we will recieve a warning in the debugger.
- //
- tempBitmapShape = GetPixMapShape(129);
-
- #ifdef kCheckBitmapShape
- GXValidateShape (tempBitmapShape);
- #endif
-
- //
- // Define the text shape to clip our bitmap shape with. In this case, our clip shape will be a text
- // shape which uses a text size of 96 point and the Helvetica font.
- //
- newClipShape = GXNewText( 5,(unsigned char*)"BEACH", nil );
- SetShapeCommonFont( newClipShape, helveticaFont );
- GXSetShapeTextSize( newClipShape, ff(96) );
-
- //
- // Determine the bounds of our bitmap and clip shape and move the clip shape to the top left corner
- // of the bitmap shape.
- //
- GXGetShapeBounds( tempBitmapShape, 0, &bitmapBounds );
- GXGetShapeBounds( newClipShape, 0, &clipShapeBounds );
-
- clipShapeHeight = clipShapeBounds.bottom - clipShapeBounds.top;
-
- GXMoveShapeTo ( newClipShape, bitmapBounds.left - ff(7), clipShapeHeight + bitmapBounds.top - ff(2) );
-
- //
- // We turn off the metrics and contour capabilites of TrueType to enable GX to
- // scale the clip shape linearly. We convert our text shape to a path shape because
- // you cannot use a text shape as a clip shape. A clip shape can only be a geometric shape
- // (i.e. a point, line, rectangle, polygon, curve, or path).
- //
- GXSetShapeTextAttributes ( newClipShape, gxNoMetricsGridText | gxNoContourGridText );
- GXPrimitiveShape ( newClipShape );
-
- //
- // Since, we just moved the clip shape, we need to determine the bounds of our shape again.
- // We can now determine the amount we need to scale our clip shape to cover our bitmap
- // shape. This is accomplished by determining the differences between the height and width
- // of the bitmap and clip shapes. We will then use these differences as the scale factors
- // when we call GXScaleShape (..) to scale up our clip shape.
- //
- // The "fl" macro converts a floating point number into a fixed point number.
- //
- GXGetShapeBounds( newClipShape, 0, &clipShapeBounds );
-
- clipShapeWidth = clipShapeBounds.right - clipShapeBounds.left;
- bitmapWidth = bitmapBounds.right - bitmapBounds.left;
- xScaleFactor = FixedToFloat( bitmapWidth ) / FixedToFloat ( clipShapeWidth );
-
- clipShapeHeight = clipShapeBounds.bottom - clipShapeBounds.top;
- bitmapHeight = bitmapBounds.bottom - bitmapBounds.top;
- yScaleFactor = FixedToFloat( bitmapHeight ) / FixedToFloat ( clipShapeHeight );
-
- GXScaleShape( newClipShape, fl(xScaleFactor), fl(yScaleFactor), bitmapBounds.left, bitmapBounds.top );
-
- //
- // Set the clip of our bitmap shape to our text shape and add it to our picture.
- //
- GXSetShapeClip( tempBitmapShape, newClipShape );
- GXSetPictureParts(gthePicture, 0, 0, 1, &tempBitmapShape, nil, nil, nil );
- GXDisposeShape ( tempBitmapShape );
-
- //
- // Change the fill of our text shape be to the outline of the text, set the size used, set the pen to cruise on the outside
- // of the contour of each letter, and add it to our picture shape.
- //
- GXSetShapeFill( newClipShape, gxClosedFrameFill );
- GXSetShapeStyleAttributes( newClipShape, gxOutsideFrameStyle );
- GXSetShapePen( newClipShape, ff(3));
- SetShapeCommonColor( newClipShape, blue);
-
- GXSetPictureParts(gthePicture, 0, 0, 1, &newClipShape, nil, nil, nil);
- GXDisposeShape ( newClipShape );
-
- GXMoveTransform(GXGetShapeTransform( gthePicture ), ff(20), ff(15) );
- }
-
-
- /*------ DoDraw ---------------------------------------------------------------------------------------*/
-
- void DoDraw(gWindow)
- WindowPtr gWindow;
- {
- GXDrawShape (gthePicture);
- }
-
-
- /*------ DoDispose -------------------------------------------------------------------------------------*/
-
- void DoDispose(gWindow)
- WindowPtr gWindow;
- {
- /**
- You should always dispose of your GX graphics objects before tossing your window. Why? It's generally good
- form and this approach guarantees that everything is disposed. If you had not disposed of everything, the
- call to DisposeWindow should dispose of the objects. If you are running the debugging version of the
- SecretGraphics init with notices set, you will receive a notice that you had not disposed of everything. You
- can turn notices on in this file by setting gDebugging = TRUE (above).
- **/
- DisposeCommonColors ();
- GXDisposeShape(gthePicture);
- GXDisposeShape(gWindowBoundsShape);
- DisposeWindow(gWindow);
- }
-
-
-
- /*------ DoClick ---------------------------------------------------------------------------------------*/
-
- void DoClick( orgMouseLoc, theWindow )
- gxPoint orgMouseLoc;
- WindowPtr theWindow;
- {
- }
-
-
- /*------ DoIdle ----------------------------------------------------------------------------------------*/
-
- void DoIdle(gWindow)
- WindowPtr gWindow;
- {
- }
-